package myJs.cps

import japgolly.scalajs.react.facade.React
import japgolly.scalajs.react.vdom.{VdomElement, VdomNode}
import slinky.readwrite._

import scala.scalajs.js

/** Created by yz on 20/10/2023
  */

trait MyScalaFnComponentCore[P] extends Any {
  type Props = P

  def component: js.Function

   def componentWithReader(propsReader: Reader[P]) = {
    component.asInstanceOf[js.Dynamic].__propsReader = propsReader.asInstanceOf[js.Object]
    component
  }

}

final class MyScalaFnComponent[P] private (val component: js.Function)
    extends AnyVal
    with MyScalaFnComponentCore[P] {

}

object MyScalaFnComponent {

  @inline def apply[P](fn: P => VdomNode) =
    new MyScalaFnComponent[P]({
      var ret: js.Function1[js.Object, React.Node] = null
      ret = (obj: js.Object) => {
        if (obj.hasOwnProperty("__")) {
          fn(obj.asInstanceOf[js.Dynamic].__.asInstanceOf[P]).rawNode
        } else {
          val reader=implicitly[Reader[DraggableModalDialogCp.Props]]
          fn(ret.asInstanceOf[js.Dynamic].__propsReader.asInstanceOf[Reader[P]].read(obj)).rawNode
        }
      }

      ret
    })

}
